package com.bl.car.controller;

import com.bl.car.constants.StateConstant;
import com.bl.car.entity.Car;
import com.bl.car.entity.Driver;
import com.bl.car.entity.Owner;
import com.bl.car.entity.User;
import com.bl.car.repositiy.CarRepository;
import com.bl.car.repositiy.DriverRepository;
import com.bl.car.repositiy.OwnerRepository;
import com.bl.car.repositiy.UserRepository;
import com.bl.car.result.Result;
import com.bl.car.utils.SpringSessionUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 审核接口
 *
 * @author zhx
 * @create 2017-11-16 15:29
 **/
@RestController
@RequestMapping("/check")
public class CheckController {

    @Autowired
    private OwnerRepository ownerRepository;

    @Autowired
    private DriverRepository driverRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CarRepository carRepository;

    //获取驾驶员列表
    @PostMapping("/getDriverList")
    public Result<List<Driver>> getDriverList(){
        List<Driver> all = driverRepository.findByUserIdNotNullOrderById();
        return Result.ok(all);
    }

    //获取业户列表
    @PostMapping("/getOwnerList")
    public Result<List<Owner>> getOwnerList(){
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        List<Owner> all = ownerRepository.findAll(sort);
        return Result.ok(all);
    }

    //获取业户申请的驾驶员列表
    @PostMapping("/getOwnerDriverList")
    public Result<List<Driver>> getOwnerDriverList(){
        List<Driver> all = driverRepository.findByOwnerIdNotNullOrderById();
        return Result.ok(all);
    }

    //获取我的驾驶员列表
    @PostMapping("/getMyDriverList")
    public Result<List<Driver>> getMyDriverList(@RequestParam("userId") String userId){
        List<Driver> all = driverRepository.findByOwnerIdOrderById("userId");
        return Result.ok(all);
    }

    //提交审核信息业户
    @PostMapping("/getCheckOwner")
    public Result<Owner> getCheckOwner(@RequestBody Owner owner,HttpSession session){
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        owner.setApplyTime(df.format(new Date()));

        User tmpUser = SpringSessionUtil.getSession(session);

        owner.setUserId( Long.toString(tmpUser.getId()));

        owner.setIsEffect(StateConstant.USER_NONE);
        ownerRepository.save(owner);

        tmpUser.setIsEffect(StateConstant.USER_CHECK);

        userRepository.save(tmpUser);

        return Result.ok(owner);
    }

    @PostMapping("/getCheckDriver")
    public Result<String> getCheckDriver(@RequestBody Driver driver,HttpSession session){
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        driver.setApplyTime(df.format(new Date()));

        User tmpUser = SpringSessionUtil.getSession(session);

        driver.setUserId( Long.toString(tmpUser.getId()));
        driver.setIsEffect(StateConstant.USER_NONE);

        driverRepository.save(driver);

        tmpUser.setIsEffect(StateConstant.USER_CHECK);

        userRepository.save(tmpUser);

        return Result.ok(driver);
    }

    //业户审核
    @PostMapping("/checkOwner")
    public Result<String> checkOwner(@RequestParam("id") Long id,@RequestParam("type") String type){

        Owner one = ownerRepository.findOne(id);

        one.setIsEffect(type);
        ownerRepository.save(one);
        User user = userRepository.findOne(Long.parseLong(one.getUserId()));
        user.setIsEffect(type);
        userRepository.save(user);

        return Result.ok("");
    }

    //驾驶员审核
    //业户审核
    @PostMapping("/checkDriver")
    public Result<String> checkDriver(@RequestParam("id") Long id,@RequestParam("type") String type){

        Driver one = driverRepository.findOne(id);

        one.setIsEffect(type);
        driverRepository.save(one);

        if(one.getUserId() != null){
            User user = userRepository.findOne(Long.parseLong(one.getUserId()));
            user.setIsEffect(type);
            userRepository.save(user);
        }


        return Result.ok("");
    }

    //车辆审核
    @PostMapping("/checkCar")
    @Transactional
    public Result<String> checkCar(@RequestParam("id") Long id,@RequestParam("type") String type){

        //注  不通过就删除

        if(type.equals(StateConstant.USER_FAILURE)){
            carRepository.delete(id);

            return Result.ok("");
        }

        Car one = carRepository.findOne(id);

        one.setIsEffect(type);
        carRepository.save(one);

        return Result.ok("");
    }

    ///注销方法

    //前端注销方法
    @PostMapping("/ownerpushdel")
    @Transactional
    public Result<String> ownerpushdel(@RequestParam("id") Long id){
        List<Driver> byOwnerIdOrderById = driverRepository.findByOwnerIdOrderById(Long.toString(id));

        if(byOwnerIdOrderById.size()>0){
            return Result.error("当前业户下存在驾驶员!请先注销驾驶员!");
        }

        Owner one = ownerRepository.getOne(id);


        one.setIsEffect(StateConstant.DELETE_TYPE);

        ownerRepository.save(one);

        return Result.ok("");
    }


    //业主提交注销

    //驾驶员提交注销
    @PostMapping("/driverpushdel")
    @Transactional
    public Result<String> driverpushdel(@RequestParam("id") Long id){

        Car byDriverId = carRepository.findByDriverId(Long.toString(id));

        if(byDriverId != null){
            return Result.error("当前驾驶员下有车辆!清先注销车辆!");
        }

        Driver one = driverRepository.getOne(id);

        one.setIsEffect(StateConstant.DELETE_TYPE);

        driverRepository.save(one);

        return Result.ok("");
    }
    //车辆提交注销
    @PostMapping("/carpushdel")
    @Transactional
    public Result<String> carpushdel(@RequestParam("id") Long id){

        Car one = carRepository.getOne(id);

        one.setIsEffect(StateConstant.DELETE_TYPE);

        carRepository.save(one);

        return Result.ok("");
    }

    //后台注销判断

    //业户注销
    @PostMapping("/ownerdel")
    @Transactional
    public Result<String> ownerdel(@RequestParam("id") Long id,@RequestParam("type") String type){
        if(type.equals(StateConstant.DELETE_TYPE)){

            Owner one = ownerRepository.getOne(id);
            ownerRepository.delete(id);

            userRepository.delete(Long.parseLong(one.getUserId()));
        }else{
            Owner one = ownerRepository.getOne(id);

            one.setIsEffect(StateConstant.USER_OWNER);

            ownerRepository.save(one);
        }
        return Result.ok("");
    }


    //驾驶员注销
    @PostMapping("/driverdel")
    @Transactional
    public Result<String> driverdel(@RequestParam("id") Long id,@RequestParam("type") String type){
        if(type.equals(StateConstant.DELETE_TYPE)){
            Driver one = driverRepository.getOne(id);

            driverRepository.delete(id);

            userRepository.delete(Long.parseLong(one.getUserId()));


        }else{
            Driver one = driverRepository.getOne(id);

            one.setIsEffect(StateConstant.USER_OWNER);

            driverRepository.save(one);
        }
        return Result.ok("");
    }


    //车辆注销
    @PostMapping("/cardel")
    public Result<String> cardel(@RequestParam("id") Long id,@RequestParam("type") String type){

        if(type.equals(StateConstant.DELETE_TYPE)){
            carRepository.delete(id);
        }else{
            Car one = carRepository.getOne(id);

            one.setIsEffect(StateConstant.CAR);

            carRepository.save(one);
        }
        return Result.ok("");
    }







}
